part of 'index.dart';

class FriendSearchProvider extends ChangeNotifier {
  final controller = TextEditingController();
  int index = 0;
  bool isSearch = false;
  List<RoomSearchModel> rooms = [];
  List<UserSearchModel> users = [];
  final roomRefreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );
  final int _limit = 15;
  int _roomPage = 1;
  bool _noMoreRoom = false;

  final userRefreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );
  int _userPage = 1;
  bool _noMoreUser = false;

  FriendSearchProvider() {
    _recommendRoom();
    _recommendUser();
  }

  bool _disposed = false;

  Future<void> _recommendRoom() async {
    final result = await FriendAPI.recommendRooms(country: 'Oman');
    if (!_disposed) {
      rooms = result;
      notifyListeners();
    }
  }

  Future<void> _recommendUser() async {
    final result = await FriendAPI.recommendUsers();
    if (!_disposed) {
      users = result;
      notifyListeners();
    }
  }

  Future<void> _onSearch(String? keyword) async {
    Tools.unfocus();
    if (controller.text.isNotEmpty) {
      FriendAPI.saveHistory(controller.text);
      notifyListeners();
    }
    if (index == 0) return await _searchRooms(keyword, isRefresh: true);
    if (index == 1) return await _searchUsers(keyword, isRefresh: true);
  }

  Future<void> _searchRooms(String? keywords, {bool isRefresh = false}) async {
    if (isRefresh) _roomPage = 1;
    keywords ??= controller.text;
    if (keywords.isEmpty) return;
    isSearch = true;
    final results = await FriendAPI.search(
      type: 0,
      keywords: keywords,
      pageNum: _roomPage,
      pageSize: _limit,
      showLoading: true,
    );
    if (!_disposed) {
      final result = results.$2;
      users = results.$1;
      if (isRefresh) {
        _noMoreUser = users.length < _limit;
      }
      if (isRefresh) rooms.clear();
      rooms.addAll(result);
      _roomPage++;
      _noMoreRoom = result.length < _limit;
      notifyListeners();
    }
  }

  Future<void> _searchUsers(String? keywords, {bool isRefresh = false}) async {
    isSearch = true;
    if (isRefresh) _userPage = 1;
    keywords ??= controller.text;
    if (keywords.isEmpty) return;
    final results = await FriendAPI.search(
      type: 0,
      keywords: keywords,
      pageNum: _userPage,
      pageSize: _limit,
      showLoading: true,
    );
    final result = results.$1;
    rooms = results.$2;
    if (isRefresh) users.clear();
    users.addAll(result);
    _userPage++;
    _noMoreUser = result.length < _limit;
    notifyListeners();
  }

  void onRoomLoading() async {
    if (_noMoreRoom || !isSearch) {
      roomRefreshController.finishLoad(IndicatorResult.noMore);
      return;
    }
    try {
      await _searchRooms(null);
      roomRefreshController.finishLoad();
    } catch (error) {
      roomRefreshController.finishLoad(IndicatorResult.fail);
    }
  }

  void onUserLoading() async {
    if (_noMoreUser || !isSearch) {
      userRefreshController.finishLoad(IndicatorResult.noMore);
      return;
    }
    try {
      await _searchUsers(null);
      userRefreshController.finishLoad();
    } catch (error) {
      userRefreshController.finishLoad(IndicatorResult.fail);
    }
  }

  @override
  void dispose() {
    _disposed = true;
    controller.dispose();
    roomRefreshController.dispose();
    userRefreshController.dispose();
    super.dispose();
  }
}
